﻿Shader "Custom/SpriteFlashLight" { 
    Properties { 
         _MainTex ("Texture", 2D) = "white" {} 
        _FlashColor ("Flash Color", Color) = (1,1,1)
        _SpriteBright ("Sprite bright", Range(1, 10)) = 4
        _FlashAngle ("Flash Angle",Range(0,360)) = 30
        _LoopTime("Loop Time",Float) = 0.7
        _Width("Width",Float) = 0.25
    } 
    SubShader
    { 
	    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} 
	    CGPROGRAM  
	    #pragma surface surf Lambert alpha exclude_path:prepass noforwardadd
	    sampler2D _MainTex;
        half4 _FlashColor;
        half _SpriteBright;
        half _FlashAngle;
        half _FlashTime;
        half _LoopTime;
        half _Width;

        struct Input   
	    {  
	        half2 uv_MainTex;  
	    };

         //核心：计算函数，角度，uv,光带的x长度，间隔，开始时间，偏移，单次循环时间 
         half inFlash(float angle,float2 uv,float xLength,int interval,int beginTime, float offX, float loopTime ) 
          { 
                //亮度值 
                float brightness = 0;
                //倾斜角 
                float angleInRad = 0.0174444 * angle;
                //当前时间 
                float currentTime = _Time.y;
                //获取本次光照的起始时间 
                int currentTimeInt = _Time.y / interval; 
                currentTimeInt *= interval;
                
                //获取本次光照的流逝时间 = 当前时间 - 起始时间 
                float currentTimePassed = currentTime - currentTimeInt; 
                if(currentTimePassed > beginTime) 
                { 
                    
                    float x0 = ( currentTimePassed - beginTime ) /loopTime;

                    float tanInverseInRad = 1.0 / tan(angleInRad); 

                    bool onLeft = ( tanInverseInRad > 0 );  
        			float xBottomFarLeft = onLeft ? 0.0 : tanInverseInRad;  
        			float xBottomFarRight = onLeft ? ( 1.0 + tanInverseInRad ) : 1.0;  

                    //设置右边界 
                    float xBottomRightBound = xBottomFarLeft + x0 * (xBottomFarRight - xBottomFarLeft); 
                    
                    //设置左边界 
                    float xBottomLeftBound = xBottomRightBound - xLength; 
                    
                    //投影至x的长度
                    float xProj = uv.x + uv.y * tanInverseInRad;

                    //此点的左边界 = 底部左边界 - 投影至x的长度 
                    float xPointLeftBound = xBottomLeftBound - xProj; 
                    //此点的右边界 = 底部右边界 - 投影至x的长度 
                    float xPointRightBound = xBottomRightBound - xProj; 
                    
                    //边界加上一个偏移 
                    xPointLeftBound += offX; 
                    xPointRightBound += offX; 
                    
                    //如果该点在区域内 
                    if(uv.x > xPointLeftBound && uv.x < xPointRightBound) 
                    { 
                        //得到发光区域的中心点 
                        float midness = ( xPointLeftBound + xPointRightBound ) / 2; 
                        
                        //趋近中心点的程度，0表示位于边缘，1表示位于中心点 
                        float rate= ( xLength -2 * abs( uv.x - midness ) ) /  ( xLength ); 
                        brightness = rate; 
                    } 
                }
                return brightness; 
        } 

        void surf (Input i, inout SurfaceOutput o)  
        {   
            fixed4 texCol = tex2D(_MainTex, i.uv_MainTex); 
            //角度，uv,光带的x长度，间隔，开始时间，偏移，单次循环时间 
            fixed brightness = inFlash(_FlashAngle,i.uv_MainTex,_Width,5,2,0.15,_LoopTime); 

            if(texCol.w >0.5) 
                o.Albedo  = texCol * _SpriteBright + _FlashColor * brightness;
            //空白区域，判定设置为 颜色的A <=0.5,输出空白 
            else 
                o.Albedo = fixed4(0,0,0,0);
            o.Alpha = texCol.a;
        }
	    ENDCG
    }
    FallBack "Unlit/Transparent"  
} 